package com.fan.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fan.reggie.common.R;
import com.fan.reggie.dto.DishDto;
import com.fan.reggie.entity.Dish;
import com.fan.reggie.entity.DishFlavor;
import com.fan.reggie.mapper.DishMapper;
import com.fan.reggie.service.DishFlavorService;
import com.fan.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Override
    @Transactional
    /*新增菜品。同时插入菜品对应的口味数据，dish， dish_flavor
    * */
    public void saveWithFlavor(DishDto dishDto) {
        this.save(dishDto);//保存菜品表
        Long id = dishDto.getId(); //获取菜品id
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(id); //给口味添加菜品ID
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors); //保存至菜品口味表dish_flavor
    }

    @Override
    //根据菜品id同时查询菜品及口味信息
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);
        return dishDto;
    }

    /*更新菜品信息同时修改口味数据
    * */
    @Override
    @Transactional
    public R<String> updateWithFlavor(DishDto dishDto) {
        //更新菜品基本数据
        //Dish dish = new Dish();
        //BeanUtils.copyProperties(dishDto,dish);
        this.updateById(dishDto);
        //dish.setUpdateTime(LocalDateTime.now());

        //更新菜品口味数据
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        /*dishFlavorService.update(queryWrapper);
        flavors.stream().map((item)->{
            dishFlavorService.updateById(item);
        })*/
        //可先删除口味数据，再添加新的数据
        dishFlavorService.remove(queryWrapper);
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors =  flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
        return R.success("修改菜品成功");
    }

    /*
    * 更新菜品状态*/
    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        LambdaUpdateWrapper<Dish> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Dish::getStatus,status).in(Dish::getId,ids);
        this.update(updateWrapper);
    }
}
